package net.ojbk.study.twobox.service;

import net.ojbk.study.twobox.beans.po.*;
import net.ojbk.study.twobox.beans.po.Class;
import net.ojbk.study.twobox.dao.ClassMapper;
import net.ojbk.study.twobox.dao.ConsultMapper;
import net.ojbk.study.twobox.dao.ReferMapper;
import net.ojbk.study.twobox.dao.StaffMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.time.LocalDate;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/4/14
 * Time: 16:35
 * Description: No Description
 */
@Service
public class StaffService extends SupperService<Staff> {
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private ReferMapper referMapper; // CRM系统咨询表
    @Autowired
    private ConsultMapper consultMapper; // CRM系统咨询工作的信息

    private boolean isCN(Date birthday, Date now) {

        LocalDateTime now_birthday = LocalDateTime.ofInstant(birthday.toInstant(), ZoneId.systemDefault());
        LocalDateTime now_time = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());
        if (now_time.getYear() < now_birthday.getYear() + 18)
            return false;
        if (now_time.getYear() > now_birthday.getYear() + 18)
            return true;

        if (now_time.getMonthValue() < now_birthday.getMonthValue())
            return false;
        if (now_time.getMonthValue() > now_birthday.getMonthValue())
            return true;

        if (now_time.getDayOfMonth() >= now_birthday.getDayOfMonth())
            return true;
        return false;

    }

    /**
     * 增
     *
     * @param o
     * @param br
     */
    @Override
    public void addOne(Staff o, BindingResult br) throws Exception {
        // 可能不同时空下两个人都刚满了18岁，但很有可能，这两个人活在地球上的天数不一样
        this.validated(br, Staff.class);
        if (!isCN(o.getBirthday(), o.getOndutydate()))
            throw new Exception("未成年！");

        if (staffMapper.getCount_byPostId(o.getPostid()) <= 0)
            throw new Exception("职务不存在！");

        staffMapper.insert(o);
    }

    /**
     * 删
     *
     * @param id
     */
    @Override
    public void deleteOne(int id) throws Exception {
//        3.2员工删除
//                --》检查班级表有没有该员工id
//                --》咨询工作表检查有没有该员工id
//                --》咨询表检查有没有该员工id
//        以上三张表检查没有该数据才删除(软删除|逻辑删除)
        Class aClass = new Class();
        aClass.setTeacherid(id);
        Refer refer = new Refer();
        refer.setUserid(id);
        Consult consult = new Consult();
        consult.setUserid(id);
        if (classMapper.selectCount(aClass) > 1 | referMapper.selectCount(refer) > 1 | consultMapper.selectCount(consult) > 1)
            throw new Exception("三张表中某些表还存在数据，不允许删除！");

        // 软删除
        Staff staff = staffMapper.selectByPrimaryKey(id);
        // 员工状态，1：在岗 0：离职
        staff.setState((short) 0);
        staffMapper.updateByPrimaryKeySelective(staff);
    }

    /**
     * 改
     *
     * @param o
     * @param br
     */
    @Override
    public void updateOne(Staff o, BindingResult br) throws Exception {
        this.validated(br, Staff.class);
        // 可能不同时空下两个人都刚满了18岁，但很有可能，这两个人活在地球上的天数不一样
        this.validated(br, Staff.class);
        if (!isCN(o.getBirthday(), o.getOndutydate()))
            throw new Exception("未成年！");

        if (staffMapper.getCount_byPostId(o.getPostid()) <= 0)
            throw new Exception("职务不存在！");

        staffMapper.updateByPrimaryKeySelective(o);
    }
}
